package com.bbmall.dwd;

import com.bbmall.SparkSessionUtil;
import org.apache.spark.sql.SparkSession;

import org.apache.spark.sql.SparkSession;

import org.apache.spark.sql.SparkSession;

import org.apache.spark.sql.SparkSession;

import org.apache.spark.sql.SparkSession;

public class OdsToDwdFactSales {
    public static void main(String[] args) {
        // 解析参数
        if (args.length < 2) {
            System.err.println("参数错误！用法：");
            System.err.println("历史全量处理：<is_full=true> <任意值>（历史数据在99991231分区）");
            System.err.println("每日增量处理：<is_full=false> <biz_date=yyyy-MM-dd>（如：false 2025-10-18）");
            System.exit(1);
        }
        boolean isFull = Boolean.parseBoolean(args[0]);
        String bizDate = args[1];

        // 动态生成ODS分区条件（转换格式：yyyy-MM-dd→yyyyMMdd）
        String odsWhereClause;
        if (isFull) {
            odsWhereClause = "ods_summary.dt = '99991231' AND ods_detail.dt = '99991231'";
        } else {
            String odsDt = bizDate.replace("-", "");
            odsWhereClause = "ods_summary.dt = '" + odsDt + "' AND ods_detail.dt = '" + odsDt + "'";
        }

        // 核心SQL：严格匹配dwd_fact_sales表结构
        String sql = (isFull ? "INSERT OVERWRITE" : "INSERT INTO") + " TABLE dwd.dwd_fact_sales PARTITION (sale_date) " +
                "SELECT " +
                // 1. 基础ID字段（与DWD表一致）
                "ods_summary.transaction_id, " +                // transaction_id (BIGINT)
                "ods_summary.store_code, " +                    // store_code (STRING)
                "ods_detail.product_code, " +                   // product_code (STRING)
                "ods_summary.member_id, " +                     // member_id (STRING)
                // 2. 时间维度字段（包含sale_quarter、day_of_week）
                "ods_summary.sale_datetime, " +                 // sale_datetime (STRING)
                "date_format(ods_summary.sale_datetime, 'yyyy-MM-dd') AS sale_date, " + // sale_date (分区字段)
                "year(ods_summary.sale_datetime) AS sale_year, " + // sale_year (INT)
                "month(ods_summary.sale_datetime) AS sale_month, " + // sale_month (INT)
                "quarter(ods_summary.sale_datetime) AS sale_quarter, " + // sale_quarter (INT)
                "weekofyear(ods_summary.sale_datetime) AS sale_week, " + // sale_week (INT)
                "dayofweek(ods_summary.sale_datetime) AS day_of_week, " + // day_of_week (INT：1=周一，7=周日)
                "CASE WHEN dayofweek(ods_summary.sale_datetime) IN (1,7) THEN 1 ELSE 0 END AS is_weekend, " + // is_weekend (TINYINT)
                // 3. 度量字段（与DWD表字段名完全一致）
                "ods_detail.sale_quantity AS sales_quantity, " + // sales_quantity (DECIMAL)
                "ods_detail.sale_amount AS sales_amount, " +     // sales_amount (DECIMAL)
                "ods_detail.cost_amount AS cost_amount, " +      // cost_amount (DECIMAL)
                // 4. 业务属性字段
                "ods_summary.cashier_id, " +                    // cashier_id (STRING)
                "ods_summary.payment_method, " +                // payment_method (STRING)
                "ods_summary.promotion_type, " +                // promotion_type (STRING)
                "ods_summary.promotion_discount, " +            // promotion_discount (DECIMAL)
                // 5. 创建时间（STRING类型）
                "ods_summary.created_time AS create_time " +    // create_time (STRING)
                // 表关联
                "FROM ods.ods_product_sales_summary ods_summary " +
                "LEFT JOIN ods.ods_product_sales_detail ods_detail " +
                "  ON ods_summary.transaction_id = ods_detail.transaction_id " +
                "WHERE " + odsWhereClause + ";";

        // 执行
        SparkSession spark = SparkSessionUtil.getSparkSession("ODS_TO_DWD_FACT_SALES");
        try {
            spark.sql(sql);
            System.out.println("销售事实表DWD层" + (isFull ? "历史全量" : "增量（" + bizDate + "）") + "处理完成！");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
            SparkSessionUtil.close();
        }
    }
}